clear all
drop _all
set mem 6500m
set more off
macro drop _all

cap cd "C:\Users\alson\Dropbox\BID_Quisqueya_conAlba\SUBMISSIONS\EHBsubmission\1ST ROUND REVISION\Replication package"

log close _all
log using pool.log, replace

global table  Tables\
global data Data\


use $data\pool.dta, clear

preserve
	collapse (count) one_index_stand, by(country)
	table country one_index_stand 
restore

preserve
	collapse (count) home, by(country)
	table country home
restore

preserve
	collapse (count) wi2_stand, by(country)
	table country wi2_stand
restore

replace country = "DR" if country == "Dominican Republic"


** Creating weights to account for different number of observations in each country

* First, generate analytical weights that will give equal weight to each country
* Assuming your country variable is called "country"
bysort country: gen n_country = _N
gen weight = 1/(6*n_country)

** Creating weights to account for different number of scales in each country

	** As much one country can have info from 5 sub-scales, each subscale weights 1/5. Assign a scale_weight to each country depending on the number of scales

gen scale_weight = 1 if country == "Colombia"
replace scale_weight = 4/5 if country == "Uruguay"
replace scale_weight = 3/5 if country == "DR" | country == "Chile"
replace scale_weight = 2/5 if country == "Ecuador"
replace scale_weight = 1/5 if country == "Nicaragua"
replace scale_weight = 2/5 if country == "Perú"

gen both_weight = weight*scale_weight

	
*Create country dummy variables
tabulate country, generate(country_dummy)

log on

    local lab: variable label one_index_stand  // Save variable label in local `lab'
   
    eststo one_index_stand_reg: areg one_index_stand female, absorb(country) vce(rob)

eststo one_index_stand_rifreg: bootstrap, seed(1234) reps(1000): rifsureg one_index_stand female country_dummy*, qs(10 25 50 75 90)

foreach k of numlist 10 25 50 75 90 {
scalar A`k' = [q`k'][female]
scalar B10 = sqrt(e(V)[1,1])
scalar B25 = sqrt(e(V)[3,3])
scalar B50 = sqrt(e(V)[5,5])
scalar B75 = sqrt(e(V)[7,7])
scalar B90 = sqrt(e(V)[9,9])
scalar I`k' = `k'
}

di "Pairwise test P10 vs P90"
test [q90]female=[q10]female
sca `var'_10_90 = r(p)

di "Pairwise test P10 vs P50"
test [q50]female=[q10]female
sca `var'_10_50 = r(p)

di "Pairwise test P50 vs P90"
test [q90]female=[q50]female
sca `var'_50_90 = r(p)


putdocx begin

putdocx paragraph

local footnotes: di "Pool. For `lab': 10th = 90th (p-value = " string(scalar(`var'_10_90), "%9.3f") "), 10th = 50th (p-value = " string(scalar(`var'_10_50), "%9.3f") "), 50th = 90th (p-value = " string(scalar(`var'_50_90), "%9.3f") ")"

putdocx text ("`footnotes'")

putdocx save domains_pvalues.docx, append

log off


** Create the global

global results_means one_index_stand_reg

global results_rifreg one_index_stand_rifreg

	esttab $results_means using "$table\tablePool.tex", replace ///
    b(%8.3f) se(%8.3f) label ///
    title("Female-Male Gaps") star(* 0.10 ** 0.05 *** 0.01) ///
    nocons noobs nonotes postfoot("") 

	
	esttab $results_rifreg using "$table\tablePool.tex", append fragment ///
    b(%8.3f) se(%8.3f) label ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    nocons nonotes nonumbers nomtitles drop(country_dummy* _cons) postfoot("\hline\hline") 
	
	
		*** POOL OF THE CORRELATION BETWEEN HOME AND SES
	
	preserve
	
	replace one_index_stand = 0 if country == "Uruguay"
	
	    eststo home_ses: reg wi2_stand home [aw=weight] if one_index_stand !=. , vce(rob) // lo q sería equivalente a hacer una correlación
		
	restore
		
		eststo ses_index: reg one_index_stand wi2_stand [aw=both_weight], vce(rob)
		
		eststo home_index: reg one_index_stand home [aw=both_weight], vce(rob)
		
		
			esttab ses_index home_index home_ses using "$table\table_pool_corrs.tex", replace ///
    b(%8.3f) se(%8.3f) label ///
    title("Pool correlations") star(* 0.10 ** 0.05 *** 0.01) ///
    drop(_cons) nonotes postfoot("")
	
	
	
		
*** SES RELATED VARIABLES 


global countries Chile Colombia DR Ecuador Nicaragua Perú Uruguay
global results

lab var primary "Some primary education"
lab var secondary "Some secondary education"
lab var tertiary "Some tertiary education"
lab var mothers_age_birth "Mother's age at birth"
lab var finhh "Father in HH"

* Clear existing results
eststo clear

foreach var of global countries {
    eststo `var': estpost summarize ///
        primary secondary tertiary mothers_age_birth finhh if country == "`var'"
	
    global results $results `var'
}

* Generate the LaTeX table
esttab using "$table\summary.tex", replace ///
    collabels(none) mtitles("Chile" "Colombia" "DR" "Ecuador" "Nicaragua" "Perú" "Uruguay") ///
    cells("mean(fmt(2))" "sd(fmt(2) par)") label ///
    nonumber varlabels("") ///
    compress


	*** Testing with OLS the use of weights
	
gen id=_n
expand 2
gen t=_n!=id
 
replace weight=1 if t==0
replace weight=both_weight if t==1
	
	
	
	    eststo one_index_stand_reg: reg one_index_stand female country_dummy* [aw=weight] if t==0
		
		est store noweights

// 		eststo one_index_stand_reg: reg one_index_stand female country_dummy* [aw=weight] if t==1
//		
// 		est store obsweights

		eststo one_index_stand_reg: reg one_index_stand female country_dummy* [aw=weight] if t==1
		
		est store bothweights
		
		suest noweights bothweights	
		
		test [noweights_mean]female=[bothweights_mean]female
		
		lab var female "Female"
	
	
	
